// revise.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        display: false,
        scrollVal: true,

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        var that = this
            //调用应用实例的方法获取全局数据
        app.getUserInfo(function(userInfo) {
            //更新数据
            that.setData({
                userInfo: userInfo
            })
        })
        var session_key = wx.getStorageSync('session_key');
        console.log(session_key);
        wx.request({
            url: app.data.baseUrl + '/api/user/user_info',
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
                session_key: session_key
            },
            success: function(res) {
                console.log(res)
                that.setData({
                    signature: res.data.data.user_info.signature,
                    video: res.data.data.video,
                    user_info: res.data.data.user_info,
                    b_img: res.data.data.b_img
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
    updateUserInfo: function() {
        console.log(app.data.baseUrl);
        var that = this;
        wx.request({
            url: app.data.baseUrl + '/user/updateUserInfo',
            data: {
                signature: that.data.signature,
                session_key: app.data.session_key
            }
        })
    },
    bindShow: function() {
        this.setData({
            display: true
        })
    },
    bindSignature: function(e) {
        let signature = e.detail.value;
        console.log(signature)
        this.setData({
            signature: signature
        });

    },
    bindVale: function() {
        var that = this;
        var session_key = wx.getStorageSync('session_key');
        console.log(session_key)
        console.log(that.data.signature)
        wx.request({
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            url: 'http://sheying.j8j0.com/api/user/updateUserInfo',
            data: {
                session_key: session_key,
                signature: that.data.signature
            },
            success: function(res) {
                console.log(res)
                that.setData({
                    value: that.data.signature
                })
            },
            fail: function(res) {
                console.log(res)
            }
        })
        this.setData({
            display: false
        })
    },
    bindquxiao: function() {
        this.setData({
            display: false
        })
    },
    listenerBtnOpenVideo: function() {
        var that = this;
        wx.chooseVideo({
            //相机和相册
            sourceType: ['album', 'camera'],
            //录制视频最大时长
            maxDuration: 60,
            //摄像头
            camera: ['front', 'back'],
            //这里返回的是tempFilePaths并不是tempFilePath
            success: function(res) {
                console.log(res.tempFilePaths[0])
                that.setData({
                    videoSource: res.tempFilePaths[0],
                    videoHidden: false,
                })
            },
            fail: function(e) {
                console.log(e)
            }
        })
    },
    addImg: function() {
        var that = this;
        var session_key = wx.getStorageSync('session_key');
        wx.chooseImage({
            count: 1,
            success: function(res) {
                console.log(res);
                var tempFilePaths = res.tempFilePaths;
                console.log(session_key)
                wx.uploadFile({
                        url: 'http://sheying.j8j0.com/api/Upload/add_img',
                        method: "POST",
                        filePath: tempFilePaths[0],
                        name: 'img',
                        header: {
                            'content-type': 'multipart/form-data'
                        },
                        formData: {
                            'session_key': wx.getStorageSync('session_key'),
                            'img_url': tempFilePaths[0]
                        },
                        success: function(res) {
                            console.log(res)
                            console.log('qqq', session_key)
                            var session_key = wx.getStorageSync('session_key');
                            console.log(session_key);
                            wx.request({
                                url: app.data.baseUrl + '/api/user/user_info',
                                method: 'POST',
                                header: {
                                    'content-type': 'application/x-www-form-urlencoded'
                                },
                                data: {
                                    session_key: session_key
                                },
                                success: function(res) {
                                    console.log(res)
                                    that.setData({
                                        signature: res.data.data.user_info.signature,
                                        video: res.data.data.video,
                                        user_info: res.data.data.user_info,
                                        b_img: res.data.data.b_img
                                    })
                                }
                            })
                        }
                    })
                    // }

            }
        })
    },
    bindbgUrl: function(e) {
        var that = this;
        console.log(e)
        wx.showModal({
            title: '是否设置为封面图',
            success: function(res) {
                if (res.confirm) {
                    console.log(res);
                    var session_key = wx.getStorageSync("session_key")
                    wx.request({
                        method: 'POST',
                        header: {
                            'content-type': 'application/x-www-form-urlencoded'
                        },
                        url: 'http://sheying.j8j0.com/api/user/set_img',
                        data: {
                            img_id: e.currentTarget.id,
                            session_key: session_key
                        },
                        success: function(res) {
                            console.log(res)
                            var plan = res.data.data;
                            var user_info = that.data.user_info;
                            user_info.plan = plan;
                            that.setData({
                                user_info: user_info
                            })
                        }
                    })
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })

    }
})